using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BCWAudioResource : MonoBehaviour
{
	private static BCWAudioResource instance;

	public static Dictionary<string, string> dictionaryClipsPath = new Dictionary<string, string>();

	public static Dictionary<string, DictionaryAudioClips> dictionaryClips = new Dictionary<string, DictionaryAudioClips>();

	private List<ListAudioSource> listAudioSource = new List<ListAudioSource>();

	public List<string> queueClip = new List<string>();

	private static string pathAssets = "Sound/ListAudioClips.asset";

	private List<string> listKeyLoading = new List<string>();

	public static AudioSource listenClip;

	public static BCWAudioResource Instance
	{
		get
		{
			if (instance == null)
			{
				Object.Instantiate(Resources.Load<GameObject>("Prefabs/Lobby/BCWAudioResource"));
			}
			return instance;
		}
	}

	private void Awake()
	{
		if (instance == null)
		{
			instance = this;
			Object.DontDestroyOnLoad(this);
			LoadingResources();
			StartCoroutine(ClipUnloaded());
			AudioSource[] componentsInChildren = GetComponentsInChildren<AudioSource>();
			ListAudioSource item = default(ListAudioSource);
			foreach (AudioSource audio in componentsInChildren)
			{
				item.key = string.Empty;
				item.audio = audio;
				listAudioSource.Add(item);
			}
		}
		else
		{
			Object.Destroy(base.gameObject);
		}
	}

	private void OnDestroy()
	{
	}

	public void PlaySound(string key, bool loop = false)
	{
		StartCoroutine(LoadingClip(key, loop, 1f, 1f));
	}

	public void PlaySound(string key, float volume, float pitch = 1f, bool loop = false)
	{
		StartCoroutine(LoadingClip(key, loop, volume, pitch));
	}

	public void Stop(string key)
	{
		for (int i = 0; i < listAudioSource.Count; i++)
		{
			if (listAudioSource[i].audio.isPlaying && listAudioSource[i].key.Equals(key))
			{
				listAudioSource[i].audio.Stop();
			}
		}
	}

	private IEnumerator LoadingClip(string key, bool loop = false, float volume = 1f, float pitch = 1f)
	{
		if (string.IsNullOrEmpty(key))
		{
			BCWDebug.LogWarning("Error: Пустое имя");
			yield break;
		}
		bool isLoaded = dictionaryClips.ContainsKey(key);
		foreach (string element in listKeyLoading)
		{
			if (element == key)
			{
				yield break;
			}
		}
		if (!isLoaded)
		{
			string pathClip;
			if (!dictionaryClipsPath.TryGetValue(key, out pathClip))
			{
				BCWDebug.LogWarning("Error: LoadingClip имя: " + key + " не существует в dictionaryClipsPath");
				yield break;
			}
			ResourceRequest tmp = null;
			if (!pathClip.Equals(string.Empty))
			{
				listKeyLoading.Add(key);
				DictionaryAudioClips dClip = new DictionaryAudioClips();
				tmp = Resources.LoadAsync(pathClip);
				yield return tmp;
				dClip.clip = tmp.asset as AudioClip;
				dClip.name = key;
				dictionaryClips.Add(key, dClip);
				listKeyLoading.Remove(key);
			}
			else
			{
				BCWDebug.LogError("BCWAudioResource LoadingClip value is null");
			}
		}
		ListAudioSource audioSource = default(ListAudioSource);
		int index = -1;
		for (int i = 0; i < listAudioSource.Count; i++)
		{
			if (listAudioSource[i].key.Equals(key) || !listAudioSource[i].audio.isPlaying)
			{
				audioSource = listAudioSource[i];
				index = i;
				if (listAudioSource[i].key.Equals(key))
				{
					break;
				}
			}
		}
		if (index != -1)
		{
			if (!listAudioSource[index].key.Equals(string.Empty))
			{
				dictionaryClips[listAudioSource[index].key].count--;
			}
			audioSource.audio.loop = loop;
			audioSource.key = key;
			audioSource.audio.clip = dictionaryClips[key].clip;
			dictionaryClips[key].count++;
			audioSource.audio.volume = volume;
			audioSource.audio.pitch = pitch;
			audioSource.audio.PlayOneShot(dictionaryClips[key].clip, volume * VolumeController.VolumeSound);
			listAudioSource[index] = audioSource;
		}
	}

	private void LoadingResources()
	{
		ListClipsForTools listClipsForTools = Resources.Load<ListClipsForTools>("Sound/ListAudioClips");
		if (listClipsForTools != null)
		{
			foreach (DictionaryClip listClip in listClipsForTools.listClips)
			{
				dictionaryClipsPath.Add(listClip.key, listClip.pathClip);
			}
			return;
		}
		BCWDebug.LogError("BCWAudioResource: Список с музыкой пуст");
	}

	private IEnumerator ClipUnloaded()
	{
		while (true)
		{
			if (queueClip.Count > 0)
			{
				string tmpName = queueClip[0];
				if (dictionaryClips[tmpName].clip != null)
				{
					Resources.UnloadAsset(dictionaryClips[tmpName].clip);
					dictionaryClips.Remove(tmpName);
					Remove(tmpName);
				}
			}
			yield return null;
		}
	}

	public void UnloadClip(string clip)
	{
		queueClip.Add(clip);
	}

	public void Remove(string clip)
	{
		queueClip.Remove(clip);
	}

	public static void PlayTestSound(AudioClip clip)
	{
		if (listenClip == null)
		{
			GameObject gameObject = Object.Instantiate(new GameObject());
			listenClip = gameObject.AddComponent<AudioSource>();
		}
		listenClip.clip = clip;
		listenClip.Play();
	}
}
